export const calcDate = (date1, date2) => {
  var date3 = date2 - date1;

  var days = Math.floor(date3 / (24 * 3600 * 1000));

  var leave1 = date3 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
  var hours = Math.floor(leave1 / (3600 * 1000));

  var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
  var minutes = Math.floor(leave2 / (60 * 1000));

  var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
  var seconds = Math.round(date3 / 1000);
  return {
    leave1,
    leave2,
    leave3,
    days: days,
    hours: hours,
    minutes: minutes,
    seconds: seconds,
  };
};

export const dateFormat = (date, format) => {
  let token = /d{1,4}|m{1,4}|yy(?:yy)?|H{1,2}|M{1,2}|S{1,2}/g;
  if (date) {
    date = date instanceof Date ? date : new Date(date);
    let flags = {
      yyyy: date.getFullYear(),
      mm: ("0" + (date.getMonth() + 1)).slice(-2),
      dd: ("0" + date.getDate()).slice(-2),
      HH: ("0" + date.getHours()).slice(-2),
      MM: ("0" + date.getMinutes()).slice(-2),
      SS: ("0" + date.getSeconds()).slice(-2),
    };
    if (!format) {
      // 默认显示格式
      format = "yyyy-mm-dd HH:MM:SS";
    }
    format = format.replace(token, function(match) {
      if (match in flags) {
        return flags[match];
      }
    });
    return format;
  }
  return;
};
